package model

import (
	"strings"
	"time"

	"github.com/bear/wolf/config"
	"github.com/google/uuid"
)

// Token 令牌模型
type Token struct {
	UserId    int      `gorm:"not null;index" json:"user_id"`
	Token     string    `gorm:"size:36;uniqueIndex;not null" json:"token"`
	ExpiresAt time.Time `gorm:"not null" json:"expires_at"`
}

// ValidateToken 验证令牌是否有效
func ValidateToken(tokenStr string) (*Token, error){
	var token Token
	//redis
	err:= config.GetValue(tokenStr,&token)
  	if err != nil {
        return nil, err // 返回错误（可能是键不存在或反序列化失败）
    }
	return &token, nil
}
//生成token
func GetToken(userId int)string{
		//生成uuid
	tokenStr :=strings.ReplaceAll(uuid.New().String(), "-", "") 
	token := &Token{
		UserId: userId,
		Token: tokenStr,
	}
	config.SetKey(tokenStr,token,30 * time.Minute)
	return tokenStr
}
